// 导入数据库操作模块
const db = require('../db/index')
// 导入日期处理函数
const dayjs = require('dayjs')

// 获取订单的处理函数
exports.getOrder = (req, res) => {
    const sql = 'select g.goods_id,g.goods_name,g.goods_img,g.goods_price,g.goods_title,g.goods_contents,c.goods_num from  ev_goods as  g left outer  join ev_cart as c on g.goods_id=c.goods_id where user_id=? and c.isChecked=1'
    // 调用db.query()执行SQL语句
    db.query(sql, req.user.id, (err, results) => {
        // 执行SQL语句失败
        if (err) return res.cc(err)
        //购物车中商品id和数量获取成功
        let num = 0
        let price = 0
        results.forEach(item => {
            num = num + item.goods_num
            price = item.goods_num * item.goods_price
            
        });
        // 生成订单号
        const tradeNo = "10003221" + Math.floor(Math.random() * 1000)
        res.send({
            code: 200,
            message: '信息获取成功',
            data: {
                orderInfoList: results,
                tradeNo,
                totalNum: num,
                totalPrice: price
            }
        })

    })

}

// 提交订单的处理函数
exports.submitOrder = (req, res) => {
    let orderInfoList = req.body.orderInfoList
    orderInfoList.forEach(item => {
        item.order_id = req.params.tradeNo
        item.user_id = req.user.id
        item.price = item.goods_price * item.goods_num
    });
    // 顺序不可以变动
    let values = orderInfoList.map(order => [order.goods_id, order.goods_num, order.price, order.order_id, order.user_id])
    const sql = 'insert into ev_order (goods_id,goods_num,price,order_id,user_id) VALUES ?'
    // const sql = 'insert into ev_order set ? ?'
    db.query(sql, [values], (err, results) => {
        // 执行SQL语句失败
        if (err) return res.cc(err)
        res.send({
            code: 200,
            message: '成功',
            data: orderInfoList
        })
    })

}

// 获取订单信息的处理函数
exports.getOrderInfo = (req, res) => {
    const sql = 'select * from ev_order where user_id=? and order_id=?'
    // 调用db.query()执行SQL语句
    db.query(sql, [req.user.id, req.params.orderId], (err, results) => {
        if (err) return res.cc(err)
        res.send({
            code: 200,
            message: '信息获取成功',
            data: results
        })
    })
}

// 更新订单支付状态的处理函数
exports.updateOrder = (req, res) => {
    // const sql = 'select * from ev_order  where user_id=? and order_id=?'
    const sql = 'update ev_order set isPay = 1 where user_id=? and order_id=?'
    db.query(sql, [req.user.id, req.params.orderId], (err, results) => {
        if (err) return res.cc(err)
        res.send({
            code: 200,
            message: '订单支付状态更新成功',
            data: results
        })

    })
}
// 根据用户获取订单信息
exports.getOrderByUserId = (req, res) => {
    const sql = 'select o.order_id,o.create_time,o.goods_id,o.goods_num,o.price,g.goods_name,g.goods_price,g.goods_img from  ev_goods as  g left outer  join ev_order as o on g.goods_id=o.goods_id where user_id=? order by o.create_time DESC'
    db.query(sql, req.user.id, (err, results) => {
        if (err) res.cc(err)
        // 将获得的订单根据订单号转为以下
        let temp = []
        let result = []
        results.forEach(obj => {
            obj.create_time = dayjs(obj.create_time).format('YYYY-MM-DD HH:mm:ss')
            if (temp.length === 0 || temp[0].order_id === obj.order_id) {
                temp.push(obj);
            } else {
                result.push(temp);
                temp = [obj];
            }
        });
        if (temp.length > 0) {
            result.push(temp);
        }

        res.send({
            code: 200,
            message: '我的订单获取成功',
            data: result
        })

    })
}
